<?php
  $yhendus=new mysqli("localhost", "if13", "ifikad", "if13_rullke");
  
  function kysiToolisteAndmed($sorttulp="worker", $otsisona=""){
    global $yhendus;
    $lubatudtulbad=array("id", "workdate", "worker_id", "worker", "assignment_id", "customer", "workdescription", "assignment", "customer_id");
    if(!in_array($sorttulp, $lubatudtulbad)){
      return "lubamatu tulp";
    }
    $otsisona=addslashes(stripslashes($otsisona));
    $kask=$yhendus->prepare("SELECT works.id, workdate, worker_id, worker, assignment_id, customer, workdescription, assignment, customer_id, w_hrs
       FROM works, workers, customers, assignments
       WHERE works.worker_id=workers.id AND works.assignment_id=assignments.id AND assignments.customer_id=customers.id
        AND (worker LIKE '%$otsisona%' OR customer LIKE '%$otsisona%' OR assignment LIKE '%$otsisona%' OR workdescription LIKE '%$otsisona%')
       ORDER BY $sorttulp");
    //echo $yhendus->error;
    $kask->bind_result($id, $workdate, $worker_id, $worker, $assignment_id, $customer, $workdescription, $assignment, $customer_id, $w_hrs);
    $kask->execute();
    $hoidla=array();
    while($kask->fetch()){
      $tootajad=new stdClass();
	  $tootajad->workdate=$workdate;
      $tootajad->worker=htmlspecialchars($worker);
      $tootajad->customer=$customer;
      $tootajad->workdescription=$workdescription;
	  $tootajad->assignment=$assignment;
	  $tootajad->w_hrs=$w_hrs;
      array_push($hoidla, $tootajad);
    }
    return $hoidla;
  }
 
  function kysiKliendiAndmed($otsisona=""){
    global $yhendus;
    $otsisona=addslashes(stripslashes($otsisona));
    $kask=$yhendus->prepare("SELECT assignments.id, customer_id, assignment, location, complete, customer
       FROM customers, assignments
       WHERE assignments.customer_id=customers.id AND customer=?
        AND (location LIKE '%$otsisona%' OR customer LIKE '%$otsisona%' OR assignment LIKE '%$otsisona%' OR complete LIKE '%$otsisona%')
       ");
    //echo $yhendus->error;
	$kask->bind_param("s", $_SESSION["realname"]);
    $kask->bind_result($id, $customer_id, $assignment, $location, $complete, $customer);
    $kask->execute();
    $hoidla=array();
    while($kask->fetch()){
      $tootajad=new stdClass();
	  $tootajad->id=$id;
	  $tootajad->customer=$customer;
      $tootajad->assignment=htmlspecialchars($assignment);
      $tootajad->location=htmlspecialchars($location);
      $tootajad->complete=$complete;
      array_push($hoidla, $tootajad);
    }
	
	
	
    return $hoidla;
  }
  
  
  /*function kysiTootajaAndmed($otsisona=""){          #Seda pole vaja, aga las ta igaks juhuks olla#
    global $yhendus;
    $otsisona=addslashes(stripslashes($otsisona));
    $kask=$yhendus->prepare("SELECT works.id, workdate, worker_id, worker, assignment_id, customer, workdescription, assignment, customer_id, w_hrs
       FROM works, workers, customers, assignments
       WHERE works.worker_id=workers.id AND works.assignment_id=assignments.id AND assignments.customer_id=customers.id
        AND (worker LIKE '%$otsisona%' OR customer LIKE '%$otsisona%' OR assignment LIKE '%$otsisona%' OR workdescription LIKE '%$otsisona%')
      ");
    //echo $yhendus->error;
	//$kask->bind_param("s", $_SESSION["realname"]);
    $kask->bind_result($id, $workdate, $worker_id, $worker, $assignment_id, $assignment, $workdescription, $w_hrs);
    $kask->execute();
    $hoidla=array();
    while($kask->fetch()){
      $tootajad=new stdClass();
	  $tootajad->workdate=$workdate;
      $tootajad->worker=htmlspecialchars($worker);
      $tootajad->customer=$customer;
      $tootajad->workdescription=$workdescription;
	  $tootajad->assignment=$assignment;
	  $tootajad->w_hrs=$w_hrs;
      array_push($hoidla, $tootajad);
    }
	
	
	
    return $hoidla;
  } */
  

  /**
  * Luuakse HTML select-valik, kus v6etakse v22rtuseks sqllausest tulnud
  * esimene tulp ning n2idatakse teise tulba oma.
  */

  function looRippMenyy($sqllause, $valikunimi, $valitudid=""){
     global $yhendus;
     $kask=$yhendus->prepare($sqllause);
     $kask->bind_result($id, $sisu);
     $kask->execute();
     $tulemus="<select name='$valikunimi'>";
     while($kask->fetch()){
       $lisand="";
       if($id==$valitudid){$lisand=" selected='selected'";}
       $tulemus.="<option value='$id' $lisand >$sisu</option>";
     }
     $tulemus.="</select>";
     return $tulemus;
  }

/*
  function looRippMenyy($sqllause, $valikunimi){
     global $yhendus;
     $kask=$yhendus->prepare($sqllause);
     $kask->bind_result($id, $sisu);
     $kask->execute();
     $tulemus="<select name='$valikunimi'>";
     while($kask->fetch()){
       $tulemus.="<option value='$id'>$sisu</option>";
     }
     $tulemus.="</select>";
     return $tulemus;
  }
*/
  
  function lisaGrupp($grupinimi){
     global $yhendus;
     $kask=$yhendus->prepare("INSERT INTO kaubagrupid (grupinimi)
                      VALUES (?)");
     $kask->bind_param("s", $grupinimi);
     $kask->execute();
  }
  
  function lisaLeping($customer_id, $assignment, $location){
     global $yhendus;
     $kask=$yhendus->prepare("INSERT INTO 
       assignments (customer_id, assignment, location)
       VALUES (?, ?, ?)");
     $kask->bind_param("iss", $customer_id, $assignment, $location);
     $kask->execute();
  }
  
  function kustutaLeping($id){
     global $yhendus;
     $kask=$yhendus->prepare("DELETE FROM assignments WHERE id=?");
     $kask->bind_param("i", $id);
     $kask->execute();
  }
  
  function muudaLeping($muudaid, $assignment, $location, $complete){
     global $yhendus;
     $kask=$yhendus->prepare("UPDATE assignments SET assignment=?, location=?, complete=? WHERE id=?");
     $kask->bind_param("ssii", $assignment, $location, $complete, $muudaid);
     $kask->execute();
  }

  //---------------  
  if( array_pop(explode("/", $_SERVER["PHP_SELF"]))=="abifunktsioonid.php"):
?>
<pre>
<?php
  print_r(kysiToolisteAndmed("hind", "fass\\aad"));
?>
</pre>
<?php endif ?>
